/*
 * 1008. 旋转字符串
 * 给定一个字符串和一个偏移量，根据偏移量旋转字符串(从左向右旋转);
 * https://www.lintcode.com/problem/rotate-string/description
 * 
 * 样例
 * 对于字符串 "abcdefg".
 * 
 * offset=0 => "abcdefg"
 * offset=1 => "gabcdef"
 * offset=2 => "fgabcde"
 * offset=3 => "efgabcd"
 * 
 * 挑战
 * 在数组上原地旋转，使用O(1)的额外空间
 * 
 * 2018.06.02 @jeyming
 */
package rotate_string;

public class rotate_string {

	/**
	 * @param str: An array of char
	 * @param offset: An integer
	 * @return: nothing
	 */
	public static void rotateString(char[] str, int offset) {
		// write your code here
		if(str.length>1) {
			offset%=str.length;
			for( int i = 0 ; i < offset ; ++i ) {
				char tmpChar = str[ str.length - 1 ];
				for ( int j = str.length-1 ; j > 0 ; --j) {
					str[ j ] = str[ j - 1 ];
				}
				str[ 0 ] = tmpChar;
			}
		}
		for(int i=0;i<str.length;++i) {
			System.out.print(str[i]+" ");
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		final char[] value= {'a','b','c','d','e','f','g'};
		rotateString(value,1000000);

	}

}
